package com.base.lib.base

import android.content.Intent
import com.base.lib.utils.Constant
import com.base.lib.utils.eventbus.EventBusHelper
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

abstract class BaseCallback(private val mBaseView: BaseView) : Callback<BaseJson?> {
    override fun onResponse(call: Call<BaseJson?>, response: Response<BaseJson?>) {
        mBaseView.dismissLoadingDialog()
        val baseJson = response.body()
        EventBusHelper.handleBaseJson(baseJson)
        if (baseJson != null) {
            if (baseJson.code != 0) {
                if (baseJson.code == 10000) {
//                    SpHelper.getInstance().clearAll()
//                    val intent = Intent()
//                    intent.setClass(mBaseView.actContext.applicationContext, LoginActivity::class.java)
//                    mBaseView.actContext.startActivity(intent)
//                    ActivityTracker.getInstance().finishAll()
                } else {
                    mBaseView.responseError(baseJson.code, baseJson.msg)
                }
            } else {
                responseSuccess(baseJson)
            }
        } else {
            mBaseView.responseError(Constant.UNKNOW_ERROR_CODE, Constant.UNKNOW_ERROR_MESSAGE)
        }
    }

    override fun onFailure(call: Call<BaseJson?>, t: Throwable) {
        mBaseView.dismissLoadingDialog()
        mBaseView.requestFailure(t)
    }

    abstract fun responseSuccess(baseJson: BaseJson?)

}